<?php

namespace app\admin\controller;

use app\admin\model\ZoneList;
use app\common\controller\Backend;
use http\Env\Request;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\Model;
use think\response\Json;
use GuzzleHttp\Client;

class Taskvideoheat extends Backend
{
    /**
     * Anchor模型对象
     * @var \app\admin\model\Anchor
     */
    protected $model = null;
    protected $zone = null;
    protected $taskModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\TaskVideoheat();

        $this->zone = new ZoneList();

        $zoneList = $this->zone->all();
        $zoneData = [];
        foreach ($zoneList as $z) {
            $zoneData[$z->zone] = $z->zonename;
        }

        $this->assignconfig("zoneList", $zoneData);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $where = json_decode($this->request->get("filter", ''), true);
        if (isset($where['zone_list.zonename']) && !empty($where['zone_list.zonename'])) {
            $where['zone_list.zone'] = $where['zone_list.zonename'];
            unset($where['zone_list.zonename']);
        }

        if (isset($where['taskBeginTime']) && !empty($where['taskBeginTime'])) {
            $time = explode(" - ", $where['taskBeginTime']);
            $where['taskBeginTime'] = ['between', [$time[0], $time[1]]];
        }

        $list = $this->model
            ->where($where)
            ->with("zoneList")
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function addjob()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //设置过滤方法
        $params = $this->request->post('row/a');
        $must_param = [
            'livestreamerUser',
            'videoURL',
            'taskBeginTime',
            'heatMinutes',
            'taskMaxNum',
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error($v.'-是必传参数');
            }
        }
        $procedure_name = 'task_videoheat_add';
        $rs = $this->model->procedure_data_add($params,$must_param,$procedure_name);
        if($rs['res']){
            $this->success();
        }else{
            $this->error(Db::getError());
        }
    }

    public function searchUser()
    {
//        try {
//            $client = new Client();
//            $id = request()->get('id');
//
//            $response = $client->request('get', 'https://tokapi-mobile-version.p.rapidapi.com/v1/user/username/' . $id, [
//                'headers' => [
//                    'X-RapidAPI-Key' => 'cac81e5590msh18226afbb3026eap1c0c45jsn607825d6c677',
//                    'X-RapidAPI-Host' => 'tokapi-mobile-version.p.rapidapi.com'
//                ]
//            ]);
//            $data = json_decode($response->getBody(), true);
//            if (isset($data) && $data['status_code'] == 0) {
//                $response = $client->request('GET', "https://tokapi-mobile-version.p.rapidapi.com/v1/post/user/".$data['uid']."/posts", [
//                    'query' => [
//                        'count' => '2',
//                        'offset' => 0
//                    ],
//                    'headers' => [
//                        'X-RapidAPI-Key' => 'cac81e5590msh18226afbb3026eap1c0c45jsn607825d6c677',
//                        'X-RapidAPI-Host' => 'tokapi-mobile-version.p.rapidapi.com'
//                    ]
//                ]);
//
//                $data = $response->getBody();
//                $data = json_decode($data, true);
//
//                if ($data['status_code'] != 0) {
//                    return json([
//                        'code' => 0,
//                        'msg' => "tokapi返回：" . $data['status_msg']
//                    ]);
//                }
//                if (isset($data['aweme_list']) && !empty($data['aweme_list'])) {
//                    $return = [];
//
//                    foreach ($data['aweme_list'] as $info) {
//                        $return[] = [
//                            'uri' => $info['video']['animated_cover']['uri'],
//                            'url_list' => $info['video']['animated_cover']['url_list'][0],
//                            'create_time' => $info['create_time']
//                        ];
//                    }
//                    $videoheatSurplusNum = Db::table('user_right')->where([
//                        'livestreamerUser' => $id
//                    ])->find();
//
//                    return json([
//                        'code' => 1,
//                        'data' => [
//                            'data' => $return,
//                            'videoheatSurplusNum' => $videoheatSurplusNum['videoheatSurplusNum'] ?? 0
//                        ]
//                    ]);
//                }
//
//                return json([
//                    'code' => 0,
//                    'msg' => "没有搜索到数据"
//                ]);
//
//            }
//
//            return json([
//                'code' => 0,
//                'msg' => 'tokapi返回：' .$data['status_msg']
//            ]);
//        } catch (Exception $ex) {
//            return json([
//                'code' => 0,
//                'msg' => $ex->getMessage()
//            ]);
//        }
        $id = request()->get('id');

        $data = Db::table('user_right')->where([
            'livestreamerUser' => $id
        ])->find();

        if (!$data)
            $this->error("没有找到用户");

        $this->success("操作成功", null, $data);
    }
}